package org.liufhframework;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.ServletException;

/**
 * @author: liufh
 * @create: 2021-08-31 21:39
 */
public class SpringApplication {

    public static void run() {
        Tomcat tomcat = new Tomcat();
        try {
            tomcat.setPort(8081);
            tomcat.getHost().setAppBase(".");
            tomcat.addWebapp("/", getAbsolutePath() + "src/main");
            tomcat.start();
            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }

    private static String getAbsolutePath() {
        String path = null;
        String folderPath = SpringApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath().substring(1);
        if (folderPath.indexOf("target") > 0) {
            path = folderPath.substring(0, folderPath.indexOf("target"));
        }
        return path;
    }

}
